import java.applet.*;
import java.awt.*;
import java.awt.event.*;
import java.awt.image.ImageObserver;

import javax.swing.*;
import java.net.*;

public class Mundo extends Applet implements Runnable,MouseListener,MouseMotionListener
{
    private Image dbImage;
    private Graphics dbg;
    private Image bGround;
    private Partes pc1;
    private Partes pc2;
    private Partes pc3;
    private Partes pc4;
    private Partes pc5;
    private Partes pc6;
    private Partes pc7;
    private Partes pc8;
    private Partes pc9; 
    private Partes pc10;    
    private Mapeamento rosa;
    private Mapeamento verde;
    private Mapeamento verdeRosa;

    private Botao btn;
    
  
    private Image pc1_img;
    private Image pc2_img;
    private Image pc3_img;
    private Image pc4_img;
    private Image pc5_img;
    private Image pc6_img;
    private Image pc7_img;
    private Image pc8_img;
    private Image pc9_img;
    private Image pc10_img;    
    private Image sil1_img;
    private Image btn_img;
    public static GameState currentState;
    
    public Mundo()
    {
    	
    }
    
    public enum GameState
    {
    	stateStoped,
    	statePlaying;
    }
    
	public void init()
	{
		this.setSize(760,420);
		MediaTracker mt = new MediaTracker(this);
		bGround = getImage(getCodeBase(),"fundo_final.gif");
		mt.addImage(bGround,0);
		try
		{
			mt.waitForID(0);
		}
		catch (InterruptedException ie)
		{
			//faz nda
		}
		
		
		verde = new Mapeamento(460, 740, 110, 320,"verde", 610, 190);
		rosa = new Mapeamento(35, 290, 110, 320,"rosa", 2, 110);
		verdeRosa = new Mapeamento(290, 500, 110, 320,"verderosa", 10, 110);

		
		
		pc1 = new Partes("peca-11.gif","verderosa",35,25,70,45, verdeRosa);
		pc1_img = getImage(getCodeBase(),pc1.getImagem());
		pc2 = new Partes("peca-12.gif","verde",155,15,70,76, verde);
		pc2_img = getImage(getCodeBase(),pc2.getImagem());
		pc3 = new Partes("peca-13.gif","rosa",280,20,77,67,rosa);
		pc3_img = getImage(getCodeBase(),pc3.getImagem());
		pc4 = new Partes("peca-14.gif","rosa",400,20,80,48, rosa);
		pc4_img = getImage(getCodeBase(),pc4.getImagem());
		pc5 = new Partes("peca-15.gif","verderosa",530,20,52,71, verdeRosa);
		pc5_img = getImage(getCodeBase(),pc5.getImagem());
		pc6 = new Partes("peca-16.gif","verde",650,20,75,62, verde);
		pc6_img = getImage(getCodeBase(),pc6.getImagem());
		pc7 = new Partes("peca-17.gif","verderosa",35,350,64,66, verdeRosa);
		pc7_img = getImage(getCodeBase(),pc7.getImagem());
		pc8 = new Partes("peca-18.gif","rosa",200,342,54,70, rosa);
		pc8_img = getImage(getCodeBase(),pc8.getImagem());
		pc9 = new Partes("peca-19.gif","verde",345,355,68,38, verde);
		pc9_img = getImage(getCodeBase(),pc9.getImagem());
		pc10 = new Partes("peca-20.gif","verderosa",490,350,50,58, verdeRosa);
		pc10_img = getImage(getCodeBase(),pc10.getImagem());

		
		
		btn = new Botao("botao.gif",600,330,148,83,this);
		btn_img = getImage(getCodeBase(),btn.getImagem());


		this.addMouseListener(this);
		this.addMouseMotionListener(this);
		currentState = GameState.stateStoped;
		
	}
	

	
	
	public void setcurrentStatePlay(){
		currentState = GameState.statePlaying;
	}
	
	public void setcurrentStateStop(){
		currentState = GameState.stateStoped;
	}
	
	public void start()
	{
		Thread th = new Thread(this);
		th.start();
	}
	
	public void stop()
	{
		
	}
	
	public void destroy()
	{
		
	}
	
	public void mouseDragged(MouseEvent e)
	{
		
		switch(currentState)
		{
		case statePlaying:
		{
		pc1.mouseDragged(e);
        pc2.mouseDragged(e);
        pc3.mouseDragged(e);
        pc4.mouseDragged(e);
        pc5.mouseDragged(e);
        pc6.mouseDragged(e);
        pc7.mouseDragged(e);
        pc8.mouseDragged(e);
        pc9.mouseDragged(e);
        pc10.mouseDragged(e);
		}
		break;
		}
	}
	
	public void mouseExited(MouseEvent e)
	{
		switch(currentState)
		{
		case statePlaying:
		{
			pc1.mouseExited(e);
	        pc2.mouseExited(e);
	        pc3.mouseExited(e);
	        pc4.mouseExited(e);
	        pc5.mouseExited(e);
	        pc6.mouseExited(e);
	        pc7.mouseExited(e);;
	        pc8.mouseExited(e);
	        pc9.mouseExited(e);
	        pc10.mouseExited(e);
		}
		break;
		}
	}
	
	public void mouseMoved(MouseEvent e)
	{
		switch(currentState)
		{
		case statePlaying:
		{

		}
		break;
		}
		
	}
	
	public void mouseEntered(MouseEvent e)
	{
		switch(currentState)
		{
		case statePlaying:
		{

		}
		break;
		}
		
	}
	
	public void mouseClicked(MouseEvent e)
	{
		switch(currentState)
		{
		case statePlaying:
		{

		}
		break;
		}
		
	}
	
	public void mouseReleased(MouseEvent e)
	{
		
        switch(currentState)
        {
        case statePlaying:
        {
    		pc1.mouseReleased(e);
            pc2.mouseReleased(e);
            pc3.mouseReleased(e);
            pc4.mouseReleased(e);
            pc5.mouseReleased(e);
            pc6.mouseReleased(e);
            pc7.mouseReleased(e);
            pc8.mouseReleased(e);
            pc9.mouseReleased(e);
            pc10.mouseReleased(e);
        }
        break;
        }
	}
	
	public void mousePressed(MouseEvent e)
	{
		
		btn.mousePressed(e);
		switch(currentState)
		{
		case statePlaying:
		{
			pc1.mousePressed(e);
	        pc2.mousePressed(e);
	        pc3.mousePressed(e);
	        pc4.mousePressed(e);
	        pc5.mousePressed(e);
	        pc6.mousePressed(e);
	        pc7.mousePressed(e);
	        pc8.mousePressed(e);
	        pc9.mousePressed(e);
	        pc10.mousePressed(e);
		}
		break;
		
		}
	}
	
	public void update(Graphics g)
	{
		if(dbImage == null)
		{
			dbImage = createImage(this.getSize().width, this.getSize().height);
			dbg = dbImage.getGraphics();
		}
		
		dbg.setColor(getBackground());
		dbg.fillRect(0,0,this.getSize().width,this.getSize().height);

		dbg.setColor(getForeground());
		paint(dbg);
		
		g.drawImage(dbImage, 0, 0, this);
	}
	
	public void run()
	{
		//Baixa prioridade para o Thread
		Thread.currentThread().setPriority(Thread.MIN_PRIORITY);
			
		//Roda um loop infinito
		while (true)
		{
			//Adicionar lógica aqui
			
			repaint();
			try
			{
				Thread.sleep(20);
			}
			catch (InterruptedException ex)
			{
				//FAZ NADA!!
			}
			
			Thread.currentThread().setPriority(Thread.MAX_PRIORITY);
		}
	}
	
	public void paint(Graphics g)
	{
		//Adicionar elementos a serem pintados
		g.drawImage(bGround,0,0,this);
	
		g.drawImage(pc1_img,pc1.getX(),pc1.getY(),this);
		g.drawImage(pc2_img,pc2.getX(),pc2.getY(),this);
		g.drawImage(pc3_img,pc3.getX(),pc3.getY(),this);
		g.drawImage(pc4_img,pc4.getX(),pc4.getY(),this);
		g.drawImage(pc5_img,pc5.getX(),pc5.getY(),this);
		g.drawImage(pc6_img,pc6.getX(),pc6.getY(),this);
		g.drawImage(pc7_img,pc7.getX(),pc7.getY(),this);
		g.drawImage(pc8_img,pc8.getX(),pc8.getY(),this);
		g.drawImage(pc9_img,pc9.getX(),pc9.getY(),this);
		g.drawImage(pc10_img,pc10.getX(),pc10.getY(),this);
		g.drawImage(btn_img,btn.getX(),btn.getY(),this);
				
	}
}
